home *** CD-ROM | disk | FTP | other *** search
/ Young Minds / Young Minds Interactive CD-ROM.ISO / sdi / save_gam.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-08  |  4.9 KB  |  185 lines

  1. /*****************************  save_game.c  ******************************/
  2. #include "sdi.h"
  3. #include <sunwindow/notify.h>
  4. #include <stdio.h>
  5.  
  6. /*
  7.  * Copyright 1987 by Mark Weiser.
  8.  * Permission to reproduce and use in any manner whatsoever on Suns is granted
  9.  * so long as this copyright and other identifying marks of authorship
  10.  * in the code and the game remain intact and visible.  Use of this code
  11.  * in other products is reserved to me--I'm working on Mac and IBM versions.
  12.  */
  13.  
  14. /*
  15.  * Code to save and restore games.
  16.  */
  17.  
  18. /* If the save format is changed, change the version number. */
  19. #define SAVE_VERSION 6
  20.  
  21. extern int time_to_play, gamemaster, cursor_type;
  22. extern char save_file_name[];
  23. extern Panel_item cursor_item, cycle_time_item;
  24. extern char *strcpy();
  25.  
  26. void
  27. save_game() {
  28.     char tmpbuf[128];
  29.     int control_x, control_y;
  30.     FILE *Savefile;
  31.  
  32.     if ((Savefile = fopen(SAVE_FILE_NAME, "r")) != NULL) {
  33.         sprintf(tmpbuf, "Save file '%s' already exists.  Overwrite?", SAVE_FILE_NAME);
  34.         if (! easy_warn(tmpbuf))
  35.             return;
  36.         fclose(Savefile);
  37.     }
  38.  
  39.     if ((Savefile = fopen(SAVE_FILE_NAME, "w")) == NULL) {
  40.         sprintf(tmpbuf, "Cannot open save file '%s'.", SAVE_FILE_NAME);
  41.         easy_pop(tmpbuf);
  42.     }
  43.  
  44.  
  45.     fprintf(Savefile, "version = %d\n", SAVE_VERSION);
  46.  
  47.     fprintf(Savefile, "level = %s\n",(char *)panel_get_value(level_item));
  48.  
  49.     fprintf(Savefile, "skill = %d\n", panel_get_value(skill_item));
  50.  
  51.     fprintf(Savefile, "interceptor_val = %d\n", panel_get_value(interceptor_item));
  52.  
  53.     fprintf(Savefile, "laser_val = %d\n", panel_get_value(laser_item));
  54.  
  55.     fprintf(Savefile, "rock_val = %d\n", panel_get_value(rock_item));
  56.  
  57.     fprintf(Savefile, "game_master = %d\n", gamemaster);
  58.  
  59.     fprintf(Savefile, "score = %s\n", panel_get_value(score_item));
  60.  
  61.     fprintf(Savefile, "name = '%s'\n", USER_NAME);
  62.  
  63.     fprintf(Savefile, "playing field size = %d, %d\n", 
  64.         window_get(cityframe, WIN_WIDTH), 
  65.         window_get(cityframe, WIN_HEIGHT));
  66.  
  67.     fprintf(Savefile, "control panel position = %d, %d\n",
  68.         (control_x = (int)window_get(controlframe, WIN_X)), 
  69.         (control_y = (int)window_get(controlframe, WIN_Y)));
  70.  
  71.     /*
  72.      * Kludge around a "feature" of Sunview in which negative
  73.      * positions are ignored.
  74.      */
  75.     window_set(controlframe, WIN_X, 0, WIN_Y, 0, 0);
  76.  
  77.     fprintf(Savefile, "city field position = %d, %d\n",
  78.         window_get(cityframe, WIN_X), 
  79.         window_get(cityframe, WIN_Y));
  80.  
  81.     fprintf(Savefile, "launch field position = %d, %d\n",
  82.         window_get(launchframe, WIN_X), 
  83.         window_get(launchframe, WIN_Y));
  84.  
  85.     window_set(controlframe, WIN_X, control_x, WIN_Y, control_y, 0);
  86.  
  87.     fprintf(Savefile, "blast_delay = %d\n", blast_delay);
  88.  
  89.     fprintf(Savefile, "cursor_type = %d\n", cursor_type);
  90.  
  91.     fclose(Savefile);
  92. }
  93.  
  94. void
  95. restore_game() {
  96.     int version;
  97.     int tmpint, w, h, x, y;
  98.     int control_x, control_y;
  99.     char tmpbuf[256];
  100.     FILE *Savefile;
  101.     if ((Savefile = fopen(SAVE_FILE_NAME, "r")) == NULL) {
  102.         sprintf(tmpbuf, "Can't open restore file '%s'.", SAVE_FILE_NAME);
  103.         easy_pop(tmpbuf);
  104.         return;
  105.     }
  106.  
  107.     fscanf(Savefile, "version = %d\n", &version);
  108.     if (version != SAVE_VERSION) {
  109.         easy_pop("The save file has the wrong version.");
  110.         return;
  111.     }
  112.  
  113.     fscanf(Savefile, "level = %s\n", tmpbuf);
  114.     panel_set_value(level_item, tmpbuf);
  115.  
  116.     fscanf(Savefile, "skill = %d\n", &tmpint);
  117.     panel_set_value(skill_item, tmpint);
  118.  
  119.     fscanf(Savefile, "interceptor_val = %d\n", &tmpint);
  120.     panel_set(interceptor_item, PANEL_VALUE, tmpint,
  121.         PANEL_MAX_VALUE, tmpint,
  122.         0);
  123.  
  124.     fscanf(Savefile, "laser_val = %d\n", &tmpint);
  125.     panel_set(laser_item, PANEL_VALUE, tmpint,
  126.         PANEL_MAX_VALUE, tmpint,
  127.         0);
  128.  
  129.     fscanf(Savefile, "rock_val = %d\n", &tmpint);
  130.     panel_set(rock_item, PANEL_VALUE, tmpint,
  131.         PANEL_MAX_VALUE, tmpint,
  132.         0);
  133.  
  134.     fscanf(Savefile, "game_master = %d\n", &gamemaster);
  135.  
  136.     fscanf(Savefile, "score = %s\n", tmpbuf);
  137.     panel_set_value(score_item, tmpbuf);
  138.  
  139.     fscanf(Savefile, "name = '%[^']'\n", tmpbuf);
  140.     if (user_name[0] == '\0')
  141.         panel_set_value(user_name_item, tmpbuf);
  142.     strcpy(user_name, tmpbuf);
  143.  
  144.     fscanf(Savefile, "playing field size = %d, %d\n", &w, &h);
  145.     /* Only set one, they track each other. */
  146.     window_set(cityframe, WIN_WIDTH, w, WIN_HEIGHT, h, 0);
  147.  
  148.     fscanf(Savefile, "control panel position = %d, %d\n",&control_x,&control_y);
  149.     /*
  150.      * Kludge around a "feature" of Sunview in which negative
  151.      * positions are ignored.
  152.      */
  153.     window_set(controlframe, WIN_X, 0, WIN_Y, 0, 0);
  154.  
  155.     fscanf(Savefile, "city field position = %d, %d\n",&x,&y);
  156.     window_set(cityframe, WIN_X, x, WIN_Y, y, 0);
  157.  
  158.     fscanf(Savefile, "launch field position = %d, %d\n",&x,&y);
  159.     window_set(launchframe, WIN_X, x, WIN_Y, y, 0);
  160.  
  161.     window_set(controlframe, WIN_X, control_x, WIN_Y, control_y, 0);
  162.  
  163.     fscanf(Savefile, "blast_delay = %d\n", &blast_delay);
  164.     panel_set_value(cycle_time_item, blast_delay);
  165.  
  166.     fscanf(Savefile, "cursor_type = %d\n", &tmpint);
  167.     cursor_type = tmpint;
  168.  
  169.     fclose(Savefile);
  170. }
  171.  
  172. FILE *
  173. getsavefile(s)
  174. char *s;
  175. {
  176.     char *filename;
  177.     FILE *stream;
  178.     Event event;
  179.     filename = SAVE_FILE_NAME;
  180.     if (((stream = fopen(filename, s)) == NULL)) {
  181.         easy_pop("Can't open the save file.");
  182.     }
  183.     return stream;
  184. }
  185.